<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.btn"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-upload>
            <el-upload
              :headers="uploadInfo.headers"
              :show-file-list="false"
              :data="uploadInfo.data"
              :on-error="uploadError"
              :on-success="importOrders"
              :before-upload="beforeUpload"
              :action="uploadInfo.action"
            >
              <el-button>excel导入</el-button>
            </el-upload>
          </template>
          <template v-slot:filter-del>
            <el-button :disabled="!canDelete" @click="del()">删除</el-button>
          </template>
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
        @pageInfo="pageInfo"
      />
    </div>
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      @returnModify="getList"
    />
    <PageDialog
      v-show="dialogInfo.visible"
      :title="dialogInfo.title"
      v-model:visible="dialogInfo.visible"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogInfo.flag">
          <el-radio label="page">当前页数据</el-radio>
          <el-radio label="all">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </PageDialog>
  </div>
</template>

<script setup name="deptEquip">
import Modify from './modify'
import {
  getPage,
  delDeptDevice,
  exportDeptDeviceExcel,
} from '@/api/cmPreMgt'
import { getDeptAll } from '@/api/core'
import axios from 'axios'
const { proxy } = getCurrentInstance()
import { ElLoading } from 'element-plus'
import { exportTemplateExcel } from '@/api/pub.js'
// 过滤相关配置
const filterInfo = ref({
  query: {
    tsSystemCode: 'LIS',
  },
  copyQuery: {},
  list: [
    {
      label: '使用部门',
      value: 'deptIdMult',
      query: {
        deptAuth: 'id',
        status: 1,
      },
      api: getDeptAll,
      componentType: 'slip-select',
      multiple: true,
      event: search,
    },
    {
      componentType: 'select-model',
      label: '对接系统',
      value: 'tsSystemCode',
      event: search,
      code: 'client_id',
    },
    {
      componentType: 'input',
      label: '设备编码',
      value: 'tsDeviceCodeLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '设备名称',
      value: 'tsDeviceNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '设备型号',
      value: 'tsStandardLike',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('deptEquip:add'),
    },
    {
      type: 'slot',
      value: 'del',
      show: proxy.$permission('deptEquip:del'),
    },
    {
      type: 'slot',
      value: 'upload',
      show: proxy.$permission('deptEquip:importExcel'),
    },
    {
      type: 'button',
      label: '下载模板',
      event: downloadTemp,
      show: proxy.$permission('deptEquip:downloadTemp'),
    },
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('deptEquip:exportExcel'),
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getPage,
  data: [],
  selections: [],
  fieldList: [
    { label: '使用部门', value: 'deptName' },
    { label: '对接系统', value: 'tsSystemName' },
    { label: '设备编码', value: 'tsDeviceCode' },
    { label: '设备名称', value: 'tsDeviceName' },
    { label: '设备型号', value: 'tsStandard' },
    { label: '厂家', value: 'tsFactoryName' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '60',
    btList: [
      {
        label: '删除',
        event: del,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('deptEquip:del'),
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
})
const uploadInfo = ref({
  action: axios.defaults.baseURL + '/exceptActual/deptDevice/import/excel',
  headers: {
    Authorization: proxy.$store.state.user.token,
  },
  loading: {},
  data: {},
})
const page = ref({
  page: true,
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
const dialogInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: 'page',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '导出',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})
const canDelete = computed(() => {
  return tableInfo.value.selections.length
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function pageInfo(obj) {
  page.value = obj
}
function handleClose() {
  dialogInfo.value.visible = false
}
function checkConfirm() {
  exportExcelList()
  dialogInfo.value.visible = false
  dialogInfo.value.msg = ''
}
function downloadTemp() {
  exportTemplateExcel('except_actual_dept_device_import').then((res) => {
    if (res) {
      proxy.$fn.parseExcel(res)
    }
  })
}
function exportExcel() {
  dialogInfo.value.visible = true
  dialogInfo.value.type = 'excel'
  dialogInfo.value.msg = '请选择数据导出范围'
  dialogInfo.value.btList[1].label = '导出'
  dialogInfo.value.flag = 'page'
}
function exportExcelList() {
  let params = {
    ...filterInfo.value.query,
    ...page.value,
    isPagerByExport: dialogInfo.value.flag === 'page' ? true : false,
  }
  exportDeptDeviceExcel(params).then((res) => {
    if (res) {
      proxy.$fn.parseExcel(res)
    }
  })
}
function uploadError(err) {
  console.log(err)
  if (err) {
    proxy.$message.error({
      message: '系统错误',
      duration: 3000,
      showClose: true,
    })
  }
  uploadInfo.value.loading.close()
}
function importOrders(resp) {
  //导入
  uploadInfo.value.loading.close()
  if (resp && resp.success) {
    proxy.$message.success({
      duration: 3500,
      showClose: true,
      message: resp.msg,
    })
    getList()
  } else {
    proxy.$message.error({
      message: resp.msg,
      duration: 3000,
      showClose: true,
    })
  }
}
function beforeUpload() {
  //发生请求时打开加载动画
  uploadInfo.value.loading = ElLoading.service({
    fullscreen: true,
    background: 'rgba(255,255,255,.4)',
  })
  return true
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//创建
function create() {
  modifyInfo.value.title = '新增部门设备对照'
  modifyInfo.value.visible = true
}
//删除
function del(row = undefined) {
  const ids = _getIds(row)
  delDeptDevice(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function _getIds(row) {
  let ids = []
  if (row && row.id) {
    ids = [row.id]
  } else {
    if (tableInfo.value.selections.length > 0) {
      ids = tableInfo.value.selections.map((row) => row.id)
    }
  }
  return ids
}
</script>

<style scoped></style>
